package com.nuumoblie.bluetoothtools.bt.sdp;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.huhushengdai.tool.log.LogTool;
import com.nuumoblie.bluetoothtools.R;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class DeviceActivity extends AppCompatActivity {

    private BluetoothDevice mBluetoothDevice;
    private TextView mUuidText;
    public final static String FULL_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";

    public SimpleDateFormat format = new SimpleDateFormat(FULL_DATE_FORMAT);
    public String fetchTime = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device);
        Intent i = getIntent();
        String deviceInfo = i.getStringExtra("device");
        mBluetoothDevice = i.getParcelableExtra("BluetoothDevice");
        mUuidText = findViewById(R.id.deviceUuid);
        ((TextView) findViewById(R.id.deviceInfo)).setText(deviceInfo);

        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_UUID);
        registerReceiver(mReceiver, filter);
    }

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            LogTool.d("onReceive = " + action);
            // 当 Discovery 发现了一个设备
            if (BluetoothDevice.ACTION_UUID.equals(action)) {
                // 从 Intent 中获取发现的 BluetoothDevice
                Log.d("uuid", "触发UUID");
                BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                Parcelable[] uuid = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
                try {
                    //反射获取本地名称
                    Method method = bluetoothDevice.getClass().getMethod("getAlias");
                    if (method != null) {
                        String localName = (String) method.invoke(bluetoothDevice);
                    }
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }

                mUuidText.setText("fetchTime:"+fetchTime+"\n");
                mUuidText.append("uuid           :"+format.format(new Date()));
                if (uuid != null) {
                    for (Parcelable ep : uuid) {
                        mUuidText.append("\n");
                        mUuidText.append(ep.toString());
                        Log.d("uuid", "UUID :" + ep.toString());
                    }
                } else {
                    mUuidText.append("\n");
                    mUuidText.setText("null");
                    Log.d("uuid", "UUID : null");
                }
            }
        }
    };

    public void createBond(View v) {
        toast("发起配对：" + mBluetoothDevice.createBond());
    }


    private void toast(String text) {
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }

    public void fetchUuids(View view) {
        fetchTime = format.format(new Date());
        toast("发起fetchUuidsWithSdp:"+mBluetoothDevice.fetchUuidsWithSdp());
    }
}